package com.igd.action.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.igd.action.BaseAction;
import com.igd.business.service.IResService;
import com.igd.common.constant.RequestConstant;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.ExtData;
import com.igd.security.Resources;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-7-5
 *
 * @description	 平台资源管理Action
 */

public class ResourcesAction extends BaseAction {

	private static final long serialVersionUID = 2947503474605861211L;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	private IResService resService;
	
	private Resources resources;
	
	/**
	 * 平台资源管理
	 * 
	 * @return
	 * 
	 * @throws IgdException
	 *  
	 * @throws SQLException 
	 */
	public String resManager() throws SQLException, IgdException{
		
		List<Resources> resList = this.getResService().getAllRes(this.getPage());
		
		this.put(RequestConstant.REQ_RES_INFO,resList);
		
		return SUCCESS;

	}

	/**
	 * 平台资源删除
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String resDel() throws SQLException, IgdException{
		
		ExtData<Resources> extData = new ExtData<Resources>();
		
		if(null != this.getResources()){
			
			this.getResService().delResources(this.getResources());

			extData.setMessage("res.del.success");

		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("res.del.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	/**
	 * 跳转到平台资源添加页面
	 * 
	 * @return
	 */
	public String toAddResources(){
		
		return SUCCESS;
	}
	
	/**
	 * 资源信息添加方法
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String addResources() throws SQLException, IgdException{
		
		ExtData<Resources> extData = new ExtData<Resources>();
		
		if(null != this.getResources()){
			
			Resources res = this.getResService().saveResources(this.getResources());
			
			ArrayList<Resources> resList = new ArrayList<Resources>();

			resList.add(res);
			
			extData.setRspData(resList);
			
			extData.setMessage("res.add.success");
			
		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("res.add.fail");
		}
		
		this.putExtData(extData);
		
		return 	this.JSON_PAGE;

	}
	
	/**
	 * 跳转到资源信息修改页
	 * 
	 * @return
	 * 
	 * @throws IgdException 
	 * 
	 * @throws SQLException 
	 */
	public String toResModify() throws SQLException, IgdException{
		
		if(StringUtils.isNotEmpty(this.getResources().getResId())){
			
			Resources res = this.getResService().getResInfoByResId(this.getResources().getResId());
			
			this.put(RequestConstant.REQ_RES_INFO,res);

		}
		
		return SUCCESS;
	}

	public String resModify() throws SQLException, IgdException{
		
		ExtData<Resources> extData = new ExtData<Resources>();
		
		if(null != this.getResources() && StringUtils.isNotEmpty(this.getResources().getResId())){
			
			this.getResService().updateResourcesByResId(this.getResources());
			
			extData.setMessage("res.modify.success");
			
		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("res.modify.fail");
		}
		
		this.putExtData(extData);
		
		return 	this.JSON_PAGE;
	}
	
	public IResService getResService() {
		return resService;
	}

	public void setResService(IResService resService) {
		this.resService = resService;
	}

	public Resources getResources() {
		return resources;
	}

	public void setResources(Resources resources) {
		this.resources = resources;
	}
}
